/*
 * Copyright 2011 Digia Oyj - Pasi Hirvonen <pasi.hirvonen@digia.fi>
 *
 *
 * This class follows the de-facto standard of XML-RPC introspection as
 * documented by http://xmlrpc-c.sourceforge.net/introspection.html.
 */

#ifndef RPCINTROSPECT_H
#define RPCINTROSPECT_H

#include <QtCore/QObject>
#include <QtCore/QPointer>
#include <QtCore/QStringList>

namespace XmlRpc {

class XmlRpcConnection;
class XmlRpcResponse;

class XmlRpcIntrospect : public QObject
{
    Q_OBJECT
public:
    explicit XmlRpcIntrospect(QPointer<XmlRpcConnection> connection);
    virtual ~XmlRpcIntrospect();

    // Returns true if the RpcIntrospect is connected to a valid XmlRpcConnection.
    bool isValid() const;

public slots:
    // Returns the method help (system.methodHelp). May contain HTML.
    // Returns true if connection is valid and the request could be sent.
    bool methodHelp(const QString &methodName);
    // Corresponds to system.listMethods XML-RPC introspection call.
    // Returns true if connection is valid and the request could be sent.
    bool listMethods();
    // Request the method signature from the remote server
    bool methodSignature(const QString &methodName);
signals:
    // Emitted when the method help has been received from the remote end.
    void methodHelpReceived(const QString &helpText);
    // Emitted when the methodList is received from the remote end.
    void methodListReceived(QStringList methods);
    // The signature is usually a list of arrays (where each array is
    // a signature). The first member is the return value, the next
    // is usually either an array of parameters or a string "paramtype1, paramtype2".
    void methodSignatureReceived(const QVariant &signature);


private slots:
    void receiveMethodHelp(XmlRpcResponse *response);
    void receiveMethodList(XmlRpcResponse *response);
    void receiveMethodSignature(XmlRpcResponse *response);

private:
    void sendRequest(const QString &method, const char *slot, const QList<QVariant> &params);

    QPointer<XmlRpcConnection> m_con;

    Q_DISABLE_COPY(XmlRpcIntrospect);
};

} // namespace XmlRpc

#endif // RPCINTROSPECT_H
